{extend name='common/container'}
{block name="content"}
<div style="margin:0 15px 15px 15px;">
<el-card shadow="never" style="min-height:650px;">
<div v-if="search_visible" id="search" class="search">
	<el-form ref="form" size="small" :model="searchData" inline>
		<el-form-item label="用户姓名">
			<el-input id="name" v-model="searchData.name"  style="width:150px;" placeholder="请输入用户姓名"></el-input>
		</el-form-item>
		<el-form-item label="用户名">
			<el-input id="user" v-model="searchData.user"  style="width:150px;" placeholder="请输入用户名"></el-input>
		</el-form-item>
		<el-form-item label="所属角色">
			<el-select   style="width:150px" v-model="searchData.role_id" filterable clearable placeholder="请选择">
				<el-option v-for="(item,i) in role_id" :key="i" :label="item.key" :value="item.val"></el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="状态">
			<el-select style="width:150px" v-model="searchData.status" filterable clearable placeholder="请选择">
				<el-option key="0" label="正常" value="1"></el-option>
				<el-option key="1" label="禁用" value="0"></el-option>
			</el-select>
		</el-form-item>
		<search-tool :search_data.sync="searchData" @refesh_list="index"></search-tool>
	</el-form>
</div>
<div class="btn-group" style="margin-top:10px;margin-bottom:10px;">
	<div>
		<el-button v-for="item in button_group" :key="item.access" v-if="checkPermission(item.access,'{:implode(',',session('admin.access'))}','{:session('admin.role_id')}',[1])" :disabled="$data[item.disabled]" :type="item.color" size="mini" :icon="item.icon" @click="fn(item.clickname)">
			<span v-if="item.batch" v-text="$data['batchUpdateStatus']?'批量保存':'批量编辑'"></span>
			<span v-else v-text="item.name"></span>
		</el-button>
	</div>
	<div><table-tool :search_visible.sync="search_visible"  @refesh_list="index"></table-tool></div>
</div>
<el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick"  row-key="user_id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" @sort-change='sortChange' v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">
	<el-table-column align="center" type="selection" width="42"></el-table-column>
	<el-table-column align="center" type = '' property="user_id"  label="编号"  width="70">
	</el-table-column>
	<el-table-column align="center"  property="name"  label="用户姓名"  width="">
	</el-table-column>
	<el-table-column align="center"  property="user"  label="用户名"  width="">
	</el-table-column>
	<el-table-column align="center"  property="role.name"  label="所属分组"  width="">
	</el-table-column>
	<el-table-column align="center"  property="note"  label="备注"  width="">
	</el-table-column>
	<el-table-column align="center"  property="status"  label="状态"  width="">
		<template slot-scope="scope">
			<el-switch @change="updateExt(scope.row,'status')" :active-value="1" :inactive-value="0" v-model="scope.row.status"></el-switch>
		</template>
	</el-table-column>
	<el-table-column align="center"  property="create_time"  label="创建时间"  width="">
		<template slot-scope="scope">
			{{parseTime(scope.row.create_time,'{y}-{m}-{d}')}}
		</template>
	</el-table-column>
	<el-table-column :fixed="ismobile()?false:'right'" label="操作" align="center" width="180">
		<template slot-scope="scope">
			<div v-if="scope.row.user_id">
				<el-button v-if="checkPermission('/admin/Adminuser/update.html','{:implode(",",session("admin.access"))}','{:session("admin.role_id")}',[1])" size="mini" icon="el-icon-edit" type="primary" @click="update(scope.row)" >修改</el-button>
				<el-button v-if="checkPermission('/admin/Adminuser/delete.html','{:implode(",",session("admin.access"))}','{:session("admin.role_id")}',[1])" size="mini" icon="el-icon-delete" type="danger" @click="del(scope.row)" >删除</el-button>
			</div>
		</template>
	</el-table-column>
</el-table>
<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
</el-card>

<!--添加-->
<Add :show.sync="dialog.addDialogStatus" size="small" @refesh_list="index"></Add>
<!--修改-->
<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refesh_list="index"></Update>
<!--查看详情-->
<Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small" @refesh_list="index"></Detail>
<!--重置密码-->
<Resetpwd :info="resetPwdInfo" :show.sync="dialog.resetPwdDialogStatus" size="small" @refesh_list="index"></ResetPwd>

</div>

{/block}
{block name="script"}
<script src="__PUBLIC__/assets/js/app.js"></script>
<script src="__PUBLIC__/components/admin/adminuser/add.js?v=<?php echo rand(1000,9999)?>"></script>
<script src="__PUBLIC__/components/admin/adminuser/update.js?v=<?php echo rand(1000,9999)?>"></script>
<script src="__PUBLIC__/components/admin/adminuser/detail.js?v=<?php echo rand(1000,9999)?>"></script>
<script src="__PUBLIC__/components/admin/adminuser/resetPwd.js?v=<?php echo rand(1000,9999)?>"></script>
<script>
new Vue({
	el: '#app',
	components:{
	},
	data: function() {
		return {
			dialog: {
				addDialogStatus : false,
				updateDialogStatus : false,
				detailDialogStatus : false,
				resetPwdDialogStatus : false,
			},
			searchData:{},
			button_group:[
				{name:'添加',color:'success',access:'/admin/Adminuser/add.html',icon:'el-icon-plus',disabled:'',clickname:'add'},
				{name:'修改',color:'primary',access:'/admin/Adminuser/update.html',icon:'el-icon-edit',disabled:'single',clickname:'update'},
				{name:'删除',color:'danger',access:'/admin/Adminuser/delete.html',icon:'el-icon-delete',disabled:'multiple',clickname:'del'},
				{name:'查看详情',color:'info',access:'/admin/Adminuser/detail.html',icon:'el-icon-view',disabled:'single',clickname:'detail'},
				{name:'重置密码',color:'primary',access:'/admin/Adminuser/resetPwd.html',icon:'el-icon-lock',disabled:'single',clickname:'resetPwd'},
			],
			loading: false,
			page_data: {
				limit: 20,
				page: 1,
				total:20,
			},
			order:'',
			sort:'',
			ids: [],
			single:true,
			multiple:true,
			search_visible:true,
			list: [],
			role_id: [],
			updateInfo:{},
			detailInfo:{},
			resetPwdInfo:{},
		}
	},
	methods:{
		index(){
			let param = {limit:this.page_data.limit,page:this.page_data.page,order:this.order,sort:this.sort}
			Object.assign(param, this.searchData,this.urlobj)
			this.loading = true
			axios.post(base_url + '/Adminuser/index',param).then(res => {
				if(res.data.status == 200){
					this.list = res.data.data.data
					this.page_data.total = res.data.data.total
					this.loading = false
					if(this.page_data.page == 1){
						this.role_id = res.data.sql_field_data.role_ids
					}
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		updateExt(row,field){
			if(row.user_id){
				axios.post(base_url + '/Adminuser/updateExt',{user_id:row.user_id,[field]:row[field]}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}
		},
		add(){
			this.dialog.addDialogStatus = true
		},
		update(row){
			let id = row.user_id ? row.user_id : this.ids.join(',')
			axios.post(base_url + '/Adminuser/getUpdateInfo',{user_id:id}).then(res => {
				if(res.data.status == 200){
					this.dialog.updateDialogStatus = true
					this.updateInfo = res.data.data
				}else{
					this.$message.error(res.data.msg)
				}
			})
		},
		del(row){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = row.user_id ? row.user_id : this.ids.join(',')
				axios.post(base_url + '/Adminuser/delete',{user_id:ids}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
						this.index()
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}).catch(() => {})
		},
		detail(row){
			this.dialog.detailDialogStatus = true
			this.detailInfo = {user_id:row.user_id ? row.user_id : this.ids.join(',')}
		},
		resetPwd(row){
			this.dialog.resetPwdDialogStatus = true
			this.resetPwdInfo = {user_id:row.user_id ? row.user_id : this.ids.join(',')}
		},
		selection(selection) {
			this.ids = selection.map(item => item.user_id)
			this.single = selection.length != 1
			this.multiple = !selection.length
		},
		handleRowClick(row, rowIndex,event){
			if(event.target.className !== 'el-input__inner'){
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		rowClass ({ row, rowIndex }) {
			for(let i=0;i<this.ids.length;i++) {
				if (row.user_id === this.ids[i]) {
					return 'rowLight'
				}
			}
		},
		sortChange(val){
			if(val.order == 'descending'){
				this.order= 'desc'
			}
			if(val.order == 'ascending'){
				this.order= 'asc'
			}
			this.sort = val.prop
			this.index()
		},
		fn(method){
			this[method](this.ids)
		},
	},
	mounted(){
		this.index()
	},
})
</script>
{/block}
